package dfs.island;

/**
 * @Description 200. 岛屿数量
 * @Author Firenut
 * @Date 2023-02-10 00:06
 */
public class T200_numIslands {
    public int numIslands(char[][] grid) {
        int count=0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]=='1'&& dfs(grid,i,j)){
                    count++;
                }
            }
        }
        return count;
    }

    boolean dfs(char[][]grid,int x,int y){
        if(!(x>=0&&y>=0&&x<grid.length&&y<grid[0].length)){
            return false;
        }
        if(grid[x][y]!='1'){
            return false;
        }

        grid[x][y]='0';
        dfs(grid,x-1,y);
        dfs(grid,x+1,y);
        dfs(grid,x,y-1);
        dfs(grid,x,y+1);
        return true;
    }
}